#!/bin/bash
#
# Copyright (c) Citrix Systems 2008. All rights reserved.
#
#     ./provision-vm vm_per_host host1 host2 ... hostN
#
# Host1 will become the master of the pool, with host2 ... hostN as slaves.
# Then, on each host, vm_per_host VMs are created, with names debian-etch-HOST_NAME-i (for i in 1..vm_per_host)

if [ $# -le 1 ]; then 
   echo "Usage: ${0} vm_per_host host1 [host2 ... hostN]"
   echo "${0} provisions <vm_per_host> debiant-etch VMs on each host and installs them on a local VHD disk. Moreover, all the hosts join a common pool." 
   echo "if PROVISION_VM_WITH_CD is set to 1, then attach guest tools ISO CD-ROM to the initial Debian Etch VM before cloning it."
   exit 1
fi 

VM_PER_HOST=$1

shift
HOSTS=$@
MASTER=$1

if [ "${PROVISION_VM_WITH_CD}" == "1" ]; then
    DEB="debian-etch-withCD"
else
    DEB="debian-etch"
fi

install-vhd () {
    HOST=$1
    XE="xe -u root -pw xenroot -s ${HOST}"
    SR=`${XE} sr-list name-label='Local storage' --minimal`
    if [ $SR ]
    then

# forget the local storage
	echo "[${HOST}] Forgeting local storage."
	PBD=`${XE} sr-list uuid=$SR  params=PBDs --minimal`
	${XE} pbd-unplug uuid=${PBD}
	${XE} sr-forget uuid=${SR}
	echo "[${HOST}] Forgotten."

# build a local VHD storage
	echo "[${HOST}] Creating a local VHD storage."
	SR=`${XE} sr-create type=ext name-label=localvhd device-config:device=/dev/sda3`
	${XE} pool-param-set uuid=$(${XE} pool-list params=uuid --minimal) default-SR=${SR} crash-dump-SR=${SR} suspend-image-SR=${SR}
	echo "[${HOST}] Created."

    fi
}

install () {
    HOST=$1
    XE="xe -u root -pw xenroot -s ${HOST}"

    echo "[${HOST}] Installing the Debian Etch VM."
    UUID=`${XE} vm-install new-name-label=${DEB} template="Debian Etch 4.0"`
    echo "[${HOST}] Installed."
    
    echo "[${HOST}] Setting the IP address and the memory size of the VM."
    NETWORK=`${XE} network-list bridge=xenbr0 --minimal`
    VIF=`${XE} vif-create vm-uuid=${UUID} network-uuid=${NETWORK} device=0`
    ${XE} vm-param-set uuid=${UUID} PV-args="noninteractive"
    ${XE} vm-param-set uuid=${UUID} memory-static-max="50MiB"
    ${XE} vm-param-set uuid=${UUID} memory-static-min="50MiB"
    ${XE} vm-param-set uuid=${UUID} memory-dynamic-max="50MiB"
    ${XE} vm-param-set uuid=${UUID} memory-dynamic-min="50MiB"
    echo "[${HOST}] Set."

    if [ "${PROVISION_VM_WITH_CD}" == "1" ]; then
        echo "[${HOST}] Attaching a CD-ROM."
        TOOLS_ISO=`${XE} vdi-list is-tools-iso=ture params=name-label --minimal`
        ${XE} vm-cd-add vm=${DEB} cd-name=${TOOLS_ISO} device=3
        echo "[${HOST}] Attached."
    fi

}

#start () {
#    HOST=$1
#    XE="xe -u root -pw xenroot -s ${HOST}"
#
#    echo "[${HOST}] Starting VM."
#    ${XE} vm-start vm="${DEB}"
#    UUID=`${XE} vm-list name-label=${DEB} params=uuid --minimal`
#
#    echo "[${HOST}] Waiting for the IP address of the VM to appear. This can take a minute or so."
#    RC=1
#    while [ ${RC} -ne 0 ]
#    do
#	sleep 10
#	IP=`${XE} vm-param-get uuid=${UUID} param-name=networks param-key="0/ip"`
#	RC=$?
#    done
#   
#    echo "[${HOST}] Debian Etch VM installed (IP=${IP})."
#}

#shutdown () {
#   HOST=$1
#   XE="xe -u root -pw xenroot -s ${HOST}"
#
#   echo "[${HOST}] Shutting down the VM."
#   ${XE} vm-shutdown vm=${DEB}
#   echo "[${HOST}] Shut down."
#}

clone () {
    HOST=$1
    XE="xe -u root -pw xenroot -s ${HOST}"

    echo  "# vm_number cumulative_time load_average vhd_size" > clone-${DEB}-${HOST}.dat
    SR=`${XE} sr-list --minimal name-label=localvhd`
    START=$(date +%s)

    for i in `seq 1 ${VM_PER_HOST}`; do 
	echo "[${HOST}] Cloning VM ${i}/${VM_PER_HOST}."
	TMP=`${XE} vm-clone vm=${DEB} new-name-label=${DEB}-${HOST}-${i}`
	CURR=$(date +%s)
	DIFF=$(( ${CURR} - ${START} ))	
	LOADAVG=`${XE} host-data-source-query data-source=loadavg host=${HOST}`
	VHDSIZE=`${XE} vdi-list --minimal sr-uuid=${SR} | sed -e 's/,/\n/g' | wc -l`
	echo "${i} ${DIFF} ${LOADAVG} ${VHDSIZE}" >> clone-${DEB}-${HOST}.dat
	echo "[${HOST}] Done."
    done
}

uninstall () {
    HOST=$1
    XE="xe -u root -pw xenroot -s ${HOST}"
    
    echo "[{$HOST}] Uninstalling the Debian Etch initial VM."
    ${XE} vm-uninstall force=true vm=${DEB}
    echo "[${HOST}] Uninstalled."
}

join-master () {
    HOST=$1
    if [ ${HOST} != ${MASTER} ]
    then
	XE="xe -u root -pw xenroot -s ${HOST}"
	echo "[${HOST}] Joining ${MASTER} pool."
	${XE} pool-join master-address=${MASTER} master-username=root master-password=xenroot;
	echo "[${HOST}] Joined."
    fi
}

#main loop
echo "Provisioning ${VM_PER_HOST} VMs on hosts: ${HOSTS} (master is ${MASTER})."
for HOST in $HOSTS; 
do 
     (install-vhd $HOST; install $HOST; clone $HOST; uninstall $HOST; join-master $HOST) &
done
